package com.codelee.soybean;

import java.sql.*;

/**
 *
 * Created by lic on 15/6/3.
 */
public abstract class AbstractEngine {


    protected boolean autoCommit = true;


    public AbstractEngine(){

    }


    public void setAutoCommit(boolean autoCommit) {
        this.autoCommit = autoCommit;
    }

    protected PreparedStatement preparedStatement(Connection conn,String sql,Object...params) throws SQLException{

        PreparedStatement statement = conn.prepareStatement(sql);

        int paramsCount = 0;
        if(params != null && params.length > 0){
            paramsCount = params.length;

        }
        ParameterMetaData pmd = statement.getParameterMetaData();
        int stateCount = pmd.getParameterCount();
        if(stateCount != paramsCount){
            throw new SQLException("Wrong number of parameters!");
        }
        for(int i=1;i<=params.length;i++){
            statement.setObject(i,params[i-1]);
        }
        return statement;
    }


}
